package com.ls.service.impl;

import com.ls.dao.UserDao;
import com.ls.entity.Role;
import com.ls.entity.User;
import com.ls.service.UserService;
import com.ls.util.SortUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public void register(User user) {
        //处理业务
        //铭文密码进行md5+salt+hash散列
        //1.生成随机盐
        String salt = SortUtils.getSalt(8);
        //2.将随机盐保存到数据库
        user.setSalt(salt);
        //3.铭文密码进行md5+salt+hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        //4.将生成的密码赋值给password
        user.setPassword(md5Hash.toHex());

        System.out.println(user);

        userDao.jiaru(user);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public User findRolesByUsername(String username) {
        return userDao.findRolesByUsername(username);
    }

    @Override
    public Role findPermsByRoleId(int id) {
        return userDao.findPermsByRoleId(id);
    }
}
